<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>缺书库</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport"	content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css"	media="all">
<link rel="stylesheet" href="../css/public.css" media="all">
<link href="../css/formSelects-v4.css" rel="stylesheet" />

</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>&nbsp;&nbsp;搜索...&nbsp;&nbsp;</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">教材id</label>
                            <div class="layui-input-inline">
                                <input type="text" name="id" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">教材名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="name" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 采购 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="Warehousing"> 添加 </button>
            </div>
        </script>
<!-- 
<input type="text" id="pageSize" name="pageSize" class="layui-input layui-hide">
<input type="text" id="allRecordNumbers" name="allRecordNumbers" class="layui-input layui-hide">
<input type="text" id="currentPageRecordNumbers" name="currentPageRecordNumbers" class="layui-input layui-hide">
 -->
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
		<script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
        </script>
    </div>
</div>
<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    var pageSize=0;
    var allRecordNumbers=0;
    var currentPageRecordNumbers=0;
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;
		options = {
	            elem: '#currentTableId',
	            url: '/Schooltextbookpurchasesystem/NeedBookQueryServlet?crud=r', //../api/table.json
	            
	            dataType: "json",
	            method: 'post',
	    	    //分页请求参数
				request: {
				    pageName: 'pageIndex', //页码的参数名称，默认：page
				    limitName: 'pageLines' //每页数据量的参数名，默认：limit
				  },  //请求数据时的参数将会变为：?pageIndex=1&pageLines=30
	    	    where: { 
	    	    	crud:"r",
	    	    },  // 传递参数
	            
	            toolbar: '#toolbarDemo',
	            defaultToolbar: ['filter', 'exports', 'print', {
	                title: '提示',
	                layEvent: 'LAYTABLE_TIPS',
	                icon: 'layui-icon-tips'
	            }],
	            cols: [[
	                {type: "checkbox", width: 50},
	                {field: 'id', width: 160, title: '教材id', sort: true},
	                {field: 'name', width: 160, title: '教材名称'},
	                {field: 'num', width: 160, title: '所缺数量', sort: true},                               
	                {title: '操作', minWidth: 200, toolbar: '#currentTableBar', align: "center"}
	            ]],
	            limits: [5, 10, 15, 20, 25, 50, 100],
	            limit: 10,          
	    		loading: true,//加载时出现加载条
	            page: true,
	            skin: 'line',
	            done:function(res,curr,count){//done这个属性是自定义属性的意思，function中接收的参数是当前表格的参数，count是当前表格的总条数，
	            	//当前页中记录总数
	            	//$("#currentPageRecordNumbers").val(res.data.length);
					//数据表中记录总数  count
	            	//$("#allRecordNumbers").val(count);	
					//获取当前页编号  curr
					//var recodePage = $(".layui-laypage-skip .layui-input").val();
					//获取当前页记录数
					//$("#pageSize").val($(".layui-laypage-limits").find("option:selected").val());
				    pageSize=$(".layui-laypage-limits").find("option:selected").val();
				    allRecordNumbers=count;
				    currentPageRecordNumbers=res.data.length;
				}       
	        };
        table.render(options);

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: result
                }
            }, 'data');

            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作

            	var index = layer.open({
                    title: '采购',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['50%', '100%'],
                    content: '../page/table/needbook_buy.html',
                });
                $(window).on("resize", function () {
                      layer.full(index);
                });                  
                
            }else if (obj.event === 'Warehousing') {  // 监听入库操作
                var index = layer.open({
                    title: '添加',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['50%', '100%'],
                    content: '../page/table/needbook_Warehousing.html',
                });
                $(window).on("resize", function () {
                      layer.full(index);
                });                   
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
        	var checkStatus = table.checkStatus('currentTableId')
            , data = checkStatus.data;
           layer.alert(JSON.stringify(data));
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;            
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '编辑',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['50%', '100%'],                    
                    content: '../page/table/needbook_edit.html',
                    success:function(layero, index){
                    	var body=layer.getChildFrame('body',index);//少了这个是不能从父页面向子页面传值的
                     	body.find('#id').val(data.id);
                  　　　　　　     	body.find('#name').val(data.name);
                          　　　　　　body.find('#num').val(data.num);
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    delete_(data.id);
                    obj.del();
                });
            }
        });
        function delete_(ids){		
    		$.ajax({
    			url: '/Schooltextbookpurchasesystem/NeedBookDeleteServlet?crud=d',
    			data: {
    				"ids":ids
    			},
    			type : 'post',
    			dataType : 'json',
    			success : function(data) {
    				if (data.code == 0) {
    					//成功后，重新加载页面
    					//$("#currentPageRecordNumbers").val($("#currentPageRecordNumbers").val()-1);
    					//$("#allRecordNumbers").val($("#allRecordNumbers").val()-1);    					
    					allRecordNumbers--;
    				    currentPageRecordNumbers--;
    					
    				    //if($("#currentPageRecordNumbers").val()==0){
    				    if(currentPageRecordNumbers==0){
    	    						table.reload('currentTableId', {
    	                        page: {
    	                            //curr: $("#allRecordNumbers").val()/$("#pageSize").val()
    	                            curr: allRecordNumbers/pageSize
    	                        }
    	                    });
    		            }
    					layer.msg('删除成功!',{icon:1,time:1500});
    				} else {
    					layer.msg(data.msg,{icon:2,time:2000});
    				}
    			},
    			error : function() {
    				layer.msg('服务器错误，请重试!',{icon:2,time:2000});
    			}
    			
    		});
    	}
    });
</script>
</body>
</html>